﻿using System;
using System.Linq;
using Whoever.Framework.CustomException;
using Whoever.Framework.Enum;
using Whoever.Patterns.Business;

using EditorialProyectos.Business.Entities;
using EditorialProyectos.DataAccess.Repositories;

namespace EditorialProyectos.Business
{
    public class CategoryManager : Manager<Category>
    {
        public CategoryManager()
            : base(RepositoryFactory.CategoryRepository)
        {
        }

        public override int Insert(Category item)
        {
            if (Repository.GetAll().Count(e => e.Name.ToUpper().Equals(item.Name.ToUpper())) > 0)
                throw new BusinessException("Category already exists", MessageType.Warning);

            return base.Insert(item);
        }

        //TODO: this kind of logic must be performed properly with context state manager
        //public override void Update(Category item)
        //{
        //    if (Repository.GetAll().Count(e => e.Description.ToUpper().Equals(item.Description.ToUpper())) > 0)
        //    {
        //        Repository.UndoChanges(item);
        //        throw new BusinessException("Category already exists", MessageType.Warning);
        //    }

        //    base.Update(item);
        //}
    }
}
